package SGEngine2

import (
	//"fmt"
	"github.com/gl"
)

type Renderer struct {
	Mat        *Material
	RenderMesh *Mesh
}

func (r *Renderer) render() {

	if r.Mat.Tex != nil {
		r.Mat.Tex.TexID.Bind(gl.TEXTURE_2D)
	}

	r.Mat.Shader.Program.Use()
	r.RenderMesh.Vertbuffer.Bind(gl.ARRAY_BUFFER)
	r.Mat.Shader.Program.GetAttribLocation("vPosition").AttribPointer(3, gl.FLOAT, false, 0, nil)
	r.Mat.Shader.Program.GetAttribLocation("vPosition").EnableArray()

	if r.RenderMesh.UV != nil {
		r.RenderMesh.UVbuffer.Bind(gl.ARRAY_BUFFER)
		r.Mat.Shader.Program.GetAttribLocation("a_texCoord").AttribPointer(2, gl.FLOAT, false, 0, nil)
		r.Mat.Shader.Program.GetAttribLocation("a_texCoord").EnableArray()
	}
	if r.RenderMesh.Color != nil {
		//fmt.Println(r.RenderMesh.Color)
		r.RenderMesh.Colorbuffer.Bind(gl.ARRAY_BUFFER)
		r.Mat.Shader.Program.GetAttribLocation("aColor").AttribPointer(4, gl.FLOAT, false, 0, nil)
		r.Mat.Shader.Program.GetAttribLocation("aColor").EnableArray()
	}

	if r.RenderMesh.MeshType == 0 {
		gl.DrawArrays(gl.TRIANGLE_STRIP, 0, len(r.RenderMesh.Vertices)/3)
	} else if r.RenderMesh.MeshType == 1 {
		gl.DrawArrays(gl.TRIANGLES, 0, len(r.RenderMesh.Vertices)/3)

	}

}
